package com.lianmai.chaincard.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lianmai.chaincard.Constants;
import com.lianmai.chaincard.R;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.youth.xframe.utils.log.XLog;
import com.youth.xframe.utils.statusbar.XStatusBar;
import com.youth.xframe.widget.XToast;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.OnClick;

import static com.lianmai.chaincard.Constants.SELECT_AREA_CODE;

/**
 * 创建日期：2018/8/15 0015
 * 作者:Kite
 * 登录界面
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.tv_code_name)
    TextView codeNameTextView;

    @BindView(R.id.edt_phone)
    EditText phoneEditView;

    @BindView(R.id.edt_password)
    EditText passwordEditView;

    private int loginType;//登录类型

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SELECT_AREA_CODE:
                if (data != null) {
                    codeNameTextView.setText(String.format("+%s", data.getStringExtra("p0")));
                }
                break;
        }
    }

    @OnClick({R.id.iv_finish, R.id.tv_code_name, R.id.tv_forget_password, R.id.tv_login,
            R.id.tv_create_account, R.id.tv_login_qq, R.id.tv_login_wx})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_finish://关闭当前界面
                finish();
                break;
            case R.id.tv_code_name://选地区
                advanceForResult(SelectAreaActivity.class, SELECT_AREA_CODE);
                break;
            case R.id.tv_forget_password://找回密码
                advance(RetrievePwdActivity.class);
                break;
            case R.id.tv_login://登录
                advance(HomePageActivity.class);
                break;
            case R.id.tv_create_account://创建账号
                advance(InvitationCodeActivity.class);
                break;
            case R.id.tv_login_qq://qq登录
                clickBtnQQLogin();
                break;
            case R.id.tv_login_wx://wx登录
                advance(ThirdWayLoginActivity.class);
                break;
        }
    }

    /**
     * qq登录
     */
    public void clickBtnQQLogin() {
        loginType = 1;
        Tencent mTencent = Tencent.createInstance(Constants.QQ_APPID, this);
        mTencent.login(this, "all", new BaseUiListener());
    }

    /**
     * 微信登录
     */
    public void clickBtnWeinxinLogin() {
        loginType = 2;
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constants.WEIXIN_APPID, false);
        // 将该app注册到微信
        api.registerApp(Constants.WEIXIN_APPID);

        //发起登录请求
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_CYB";
        api.sendReq(req);
    }

    class BaseUiListener implements IUiListener {
        public void onComplete(Object response) {
            XLog.e("QQ登录回调:" + response.toString());
            loginToQQ(response.toString());

            /**
             * 下面隐藏的是用户登录成功后 登录用户数据的获取的方法
             * 共分为两种  一种是简单的信息的获取,另一种是通过UserInfo类获取用户较为详细的信息
             *有需要看看
             * */
//                try {
//                    //获得的数据是JSON格式的，获得你想获得的内容
//                    //如果你不知道你能获得什么，看一下下面的LOG
//                    Log.v("----TAG--", "-------------" + response.toString());
//                    openidString = ((JSONObject) response).getString("openid");
//                    mTencent.setOpenId(openidString);
//
//                    mTencent.setAccessToken(((JSONObject) response).getString("access_token"), ((JSONObject) response).getString("expires_in"));
//
//
//                    Log.v("TAG", "-------------" + openidString);
//                    //access_token= ((JSONObject) response).getString("access_token");              //expires_in = ((JSONObject) response).getString("expires_in");
//                } catch (JSONException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }

            /**到此已经获得OpneID以及其他你想获得的内容了
             QQ登录成功了，我们还想获取一些QQ的基本信息，比如昵称，头像什么的，这个时候怎么办？
             sdk给我们提供了一个类UserInfo，这个类中封装了QQ用户的一些信息，我么可以通过这个类拿到这些信息
             如何得到这个UserInfo类呢？  *//*

            QQToken qqToken = mTencent.getQQToken();
            UserInfo info = new UserInfo(getApplicationContext(), qqToken);

            //    info.getUserInfo(new BaseUIListener(this,"get_simple_userinfo"));
            info.getUserInfo(new IUiListener() {
                @Override
                public void onComplete(Object o) {
                    //用户信息获取到了

                    try {

                        Toast.makeText(getApplicationContext(), ((JSONObject) o).getString("nickname")+((JSONObject) o).getString("gender"), Toast.LENGTH_SHORT).show();
                        Log.v("UserInfo",o.toString());
                        Intent intent1 = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent1);
                        finish();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(UiError uiError) {
                    Log.v("UserInfo","onError");
                }

                @Override
                public void onCancel() {
                    Log.v("UserInfo","onCancel");
                }
            });*/

        }

        @Override
        public void onError(UiError uiError) {
            XToast.error("QQ登录，onError");
        }

        @Override
        public void onCancel() {
            XToast.info("QQ登录，onCancel");
        }

    }

    public void loginToQQ(String data) {
        //HttpUtils.loginToQQ(data, getTokenCallback());
    }

}


